package com.kingja.loadsir.callback;
import com.kingja.loadsir.utils.Log;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.io.*;


public abstract class Callback implements Serializable {
    private Component rootView;
    private Context context;
    private OnReloadListener onReloadListener;
    private boolean successViewVisible;

    public Callback() {
    }

    Callback(Component view, Context context, OnReloadListener onReloadListener) {
        this.rootView = view;
        this.context = context;
        this.onReloadListener = onReloadListener;
    }

    public Callback setCallback(Context context, OnReloadListener onReloadListener) {
        this.context = context;
        this.onReloadListener = onReloadListener;
        return this;
    }

    public Component getRootView() {
        int resId = onCreateView();
        if (resId == 0 && rootView != null) {
            return rootView;
        }

        if (onBuildView(context) != null) {
            rootView = onBuildView(context);
        }

        if (rootView == null) {
            rootView = LayoutScatter.getInstance(context).parse(onCreateView(), null, false);

        }
        rootView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (onReloadEvent(context, rootView)) {
                    return;
                }
                if (onReloadListener != null) {
                    onReloadListener.onReload(component);
                }
            }
        });
        onViewCreate(context, rootView);
        return rootView;
    }

    protected Component onBuildView(Context context) {
        return null;
    }

    /**
     * if return true, the successView will be visible when the view of callback is attached.
     *
     * @return boolean
     */
    public boolean getSuccessVisible() {
        return successViewVisible;
    }

    void setSuccessVisible(boolean visible) {
        this.successViewVisible = visible;
    }

    /**
     * onRetry
     *
     * @param context Context
     * @param view    Component
     * @return boolean
     */
    protected boolean onRetry(Context context, Component view) {
        return false;
    }

    protected boolean onReloadEvent(Context context, Component view) {
        return false;
    }

    public Callback copy() {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        Object obj = null;
        try {
            oos = new ObjectOutputStream(bao);
            oos.writeObject(this);
            oos.close();
            ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            obj = ois.readObject();
            ois.close();
        } catch (Exception e) {
            Log.log(e.getMessage());
        }
        return (Callback) obj;
    }

    /**
     * obtainRootView
     *
     * @return Component
     */
    public Component obtainRootView() {
        if (rootView == null) {
            //rootView = Component.inflate(context, onCreateView(), null);
            rootView = LayoutScatter.getInstance(context).parse(onCreateView(), null, false);
        }
        return rootView;
    }

    public interface OnReloadListener extends Serializable {
        void onReload(Component v);
    }

    protected abstract int onCreateView();

    /**
     * onViewCreate
     *
     * @param context Context
     * @param view    Component
     */
    protected void onViewCreate(Context context, Component view) {
    }

    /**
     * onAttach
     *
     * @param context Context
     * @param view    Component
     */
    public void onAttach(Context context, Component view) {
    }

    /**
     * Called when the rootView of Callback is removed from its LoadLayout.
     *
     * @since 1.2.2
     */
    public void onDetach() {
    }

}
